// pages/Plan/Plan.js
let app=getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:"0",
    time:'0:00',
    planText:'',
    dayindex:'1',
    weekindex:'1',
    tabindex:0,
    hour:0,
    minute:0,
    day:["1日","2日","3日","4日","5日","6日","7日","8日","9日","10日","11日","12日","13日","14日","15日","16日","17日","18日","19日","20日","21日","22日","23日","24日","25日","26日","27日","28日","29日","30日","31日"],
    week:["周一","周二","周三","周四","周五","周六","周七"]
  },
  //获取活动名称
  getPlanTitle(e)
  {
    console.log(e.detail);
    this.data.planText=e.detail;
  },
//获取日期
ondate(event)
{
  this.data.tabindex=event.detail.index;
},
bindPickerChangeday: function(e) {
    this.setData({
      index: e.detail.value
    })
    this.data.dayindex=parseInt(e.detail.value)+1;
  },
  bindPickerChangeweek: function(e) {
    this.setData({
      index: e.detail.value
    })
    this.data.weekindex=parseInt(e.detail.value)+1;
  },
  //获取时间
  bindTimeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
    let timeArr=this.data.time.split(":");
    this.data.hour=timeArr[0];
    this.data.minute=timeArr[1];
  },
  getPlanid()
    {
        var result = "";
        var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        var charactersLength = characters.length;
        for (var i = 0; i < 2; i++) {
          result += characters.charAt(
            Math.floor(Math.random() * charactersLength)
          );
        }
        var dat = util.formatDate(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据
     let date=dat.replace(/-/g, "");
      let planid=result+date;
      return planid;
    },
    getXiaoxi()
  {
    let that=this;
    let temId = 'E5hcsS32U9hqWHtqjocQ5lXOOjOlV97Wc26vNOSBu34' // 小程序配置模板信息-模板ID
wx.getSetting({
	withSubscriptions: true,//是否同时获取用户订阅消息的订阅状态，默认不获取
	success: (res)=> {
		console.log(res)
		if (res.subscriptionsSetting && res.subscriptionsSetting.itemSettings &&
			res.subscriptionsSetting.itemSettings[temId] == "reject"){
			//打开设置去设置
			this.openConfirm('检测到您没打开推送权限，是否去设置打开？')
		}else {
			wx.requestSubscribeMessage({
				tmplIds: [temId],
				success: (res)=> {
					if (res[temId] == 'accept'){
            wx.showToast({
              title: '订阅成功！',
              icon:"success"
            })
            that.addPlan();
					}
				},
				fail: (res)=> { console.info(res) },
			})
		}
	}
})
  },
  //添加计划到数据库--------------数据库尚未添加，参数未完成！！！！！
  addPlan()
  {
    let that=this;
    let planidresult=that.getPlanid();
    let dataresult={
      hour:that.data.hour,
      minute:that.data.minute,
      plandetail:that.data.planText,
      openid:app.globalData.openid,
      planid:planidresult,
      kindofPlan:1,
      plantime:"每天 "+that.data.time,
    }
    if(this.data.tabindex==0)
    {
      dataresult.kindofPlan=3;
      dataresult["day"]=this.data.dayindex;
      dataresult.plantime="每月"+that.data.day[this.data.dayindex-1]+" "+that.data.time;
    }
    if(this.data.tabindex==1)
    {
      dataresult.kindofPlan=2;
      dataresult["week"]=this.data.weekindex;
      dataresult.plantime="每周"+that.data.week[this.data.weekindex-1]+" "+that.data.time;
    }
    wx.request({
      url:app.globalData.url+'/joinPlan', 
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' },
      data:dataresult,
       method: 'post',
       success: function (res) {
            wx.navigateBack({
              delta: 1,
            })
       },
       fail:function(err){//请求失败之后要做什么
          console.log('请求失败')
          }    
  })

  },
  onShareAppMessage: function () {

  }
})